﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：李刚 
    //
    // 文件名：DistributionCompanyEdit.cs
    // 文件功能描述：管理医疗机构的配送企业
    // 创建标识：李刚2009-04-13
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.DistributionManage
{
    public partial class DistributionCompanyEdit : BasePage
    {
        IBLL_Hospital hospitalCtrl = Factory.BLL_Hospital();
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_Distribution distCtrl = Factory.BLL_Distribution();

        public DistributionCompanyEdit()
        {
            this.ModuleNo = Module.配送关系管理.ToString();
            this.PageOperateType = OperateType.编辑.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            string hospitalId = Request.QueryString["hospitalId"];

            if (string.IsNullOrEmpty(procureCatalogId) || string.IsNullOrEmpty(hospitalId))
            {
                PageError("参数错误！", this.ReturnUrl);
            }

            if (!Page.IsPostBack)
            {
                HZ.DrugTrade.Model.Query.Hospital qHospital = new HZ.DrugTrade.Model.Query.Hospital();
                qHospital.SortFields = "HospitalID DESC";
                qHospital.HospitalID = hospitalId;
                int totalCount = 0;
                IList<HZ.DrugTrade.Model.Hospital> hospitalList = hospitalCtrl.GetHospitalByWhere(qHospital, 1, 0, out totalCount);
                if (hospitalList.Count > 0)
                {
                    HZ.DrugTrade.Model.Hospital hospital = hospitalList[0];

                    this.lblHospitalName.Text = hospital.HospitalName;
                    this.lblArea.Text = hospital.Area.AreaName;
                    this.lblHospitalAddress.Text = hospital.HospitalAddress;
                    this.lblContactPerson.Text = hospital.ContactPerson;
                    this.lblContactTel.Text = hospital.ContactTel;
                }
                else
                {
                    PageError("医疗机构不存在", this.ReturnUrl);
                }

                //绑定医疗机构的配送企业 注意：两个绑定不可颠倒
                BindDistributionCompanyList();

                //绑定所有可选的配送企业
                BindAllCompanyList();
                
            }
        }

        /// <summary>
        /// 绑定所有可选的配送企业
        /// </summary>
        private void BindAllCompanyList()
        {
            string procureCatalogId = Request.QueryString["ProcureCatalogID"];
            HZ.DrugTrade.Model.View.ProcureCatalog procureCatalog = pbCtrl.GetProcureCatalogByID(procureCatalogId);

            string companyName = procureCatalog.CompanyNames_PS;
            string companyID = procureCatalog.CompanyIDs_PS;
            string[] arrayNames = companyName.Split(',');
            string[] arrayIds = companyID.Split(',');
            for (int i = 0; i < arrayNames.Length; i++)
            {
                //仅绑定可选的企业
                if (!string.IsNullOrEmpty(arrayIds[i]) && !CheckCompanyIsOptional(arrayIds[i]))
                {
                    ListItem myItem = new ListItem();
                    myItem.Text = arrayNames[i];
                    myItem.Value = arrayIds[i];
                    this.ListBox2.Items.Add(myItem);
                }
            }
        }

        /// <summary>
        /// 绑定医疗机构的配送企业
        /// </summary>
        private void BindDistributionCompanyList()
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            string hospitalId = Request.QueryString["hospitalId"];

            HZ.DrugTrade.Model.Query.DistributionRelation aDistributionRelation = new HZ.DrugTrade.Model.Query.DistributionRelation();
            aDistributionRelation.ProcureCatalogID = procureCatalogId;
            aDistributionRelation.HospitalID = hospitalId;
            aDistributionRelation.SortFields = "CompanyName_PS asc";

            int totalCount = 0;
            IList<HZ.DrugTrade.Model.View.DistributionRelation> distibutionRelationList = distCtrl.GetDistributionByWhere(aDistributionRelation, 1, 0, out totalCount);

            string strCompanyId = "";
            string strUsingCompanyId = "";

            for (int i = 0; i < distibutionRelationList.Count; i++)
            {
                HZ.DrugTrade.Model.View.DistributionRelation distRelation = distibutionRelationList[i];

                ListItem myItem = new ListItem();

                myItem.Text = distRelation.CompanyName_PS;
                myItem.Value = distRelation.CompanyID_PS;
                if (distRelation.DistributionIsUsing == true)
                {
                    //设置不可编辑
                    myItem.Attributes.Add("class", "noselect");
                    myItem.Attributes.Add("disabled", "disabled");
                    myItem.Text = distRelation.CompanyName_PS + "（在使用）";

                    //记住不可编辑的企业
                    strUsingCompanyId += distRelation.CompanyID_PS + ",";
                }
                this.ListBox1.Items.Add(myItem);

                //
                strCompanyId += distRelation.CompanyID_PS + ",";
            }

            //存储到inputhidden
            this.hdnCompanyId.Value = strCompanyId;
            this.hdnUsingCompanyId.Value = strUsingCompanyId;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string procureCatalogId = Request.QueryString["procureCatalogId"];
            string hospitalId = Request.QueryString["hospitalId"];

            string strCompanyIds = this.hdnCompanyId.Value.TrimEnd(',');
            string[] arrayIds = strCompanyIds.Split(',');

            IList<DistributionRelation> drList = new List<DistributionRelation>();
            foreach (string comId in arrayIds)
            {
                //可更新的企业才被更新
                if (!CheckIsUsingCompany(comId))
                {
                    DistributionRelation model = new DistributionRelation();
                    model.ProcureCatalogID = procureCatalogId;
                    model.HospitalID = hospitalId;
                    model.CompanyID_PS = comId;
                    //todo:是否在次提供
                    //model.Price = 0;
                    //model.FlowID = "";
                    drList.Add(model);
                }
            }
            //执行更新
            if (distCtrl.AddDistributionByProcureCatalogHospital_Batch(drList, new DistributionRelation() { ProcureCatalogID = procureCatalogId, HospitalID = hospitalId }) == 1)
            {
                PageInfo("配送企业更新成功！", this.ReturnUrl);
            }
            else
            {
                PageError("操作失败！", this.ReturnUrl);
            }
        }

        /// <summary>
        /// 检查企业是否是在使用配送企业
        /// </summary>
        /// <param name="comId"></param>
        /// <returns></returns>
        private bool CheckIsUsingCompany(string comId)
        {
            string strUsingCompanyIds = this.hdnUsingCompanyId.Value.TrimEnd(',');
            string[] arrayIds = strUsingCompanyIds.Split(',');

            foreach (string usingId in arrayIds)
            {
                if (usingId == comId && !string.IsNullOrEmpty(comId))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 检查企业是否已被医疗机构关联
        /// </summary>
        /// <returns></returns>
        private bool CheckCompanyIsOptional(string comId)
        {
            string strCompanyIds = this.hdnCompanyId.Value.TrimEnd(',');
            string[] arrayIds = strCompanyIds.Split(',');

            foreach (string usingId in arrayIds)
            {
                if (usingId == comId && !string.IsNullOrEmpty(comId))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
